var t = require('../../../@babel/runtime/helpers/defineProperty');
Component({
  data: {
    canIUse: !!wx.chooseMessageFile,
    placeholder:
      "data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='300' height='225'/>",
    ctrl: [],
  },
  properties: { nodes: Array, lazyLoad: Boolean, loading: String },
  methods: {
    play: function (t) {
      if (
        (this.top.group && this.top.group.pause(this.top.i),
        this.top.videoContexts.length > 1 && this.top.data.autopause)
      )
        for (var e = this.top.videoContexts.length; e--; )
          this.top.videoContexts[e].id != t.currentTarget.id &&
            this.top.videoContexts[e].pause();
    },
    playVideo: function (e) {
      var o = e.currentTarget.dataset,
        i = o.i,
        a = (o.n, o.id);
      console.log(this.top),
        this.top.videoContexts
          .find(function (t) {
            return t.id == a;
          })
          .play(),
        this.setData(t({}, 'nodes['.concat(i, '].wasPlay'), !0));
    },
    imgtap: function (t) {
      var e = t.currentTarget.dataset.attrs;
      if (!e.ignore) {
        var o = !0;
        if (
          (this.top.triggerEvent('imgtap', {
            id: t.currentTarget.id,
            src: e.src,
            ignore: function () {
              return (o = !1);
            },
          }),
          o)
        ) {
          if (this.top.group) return this.top.group.preview(this.top.i, e.i);
          var i = this.top.imgList,
            a = i[e.i] ? i[e.i] : ((i = [e.src]), e.src);
          wx.previewImage({ current: a, urls: i });
        }
      }
    },
    handleNodeToTextAndCopy: function (t) {
      var e = '';
      !(function t(o) {
        for (var i in o)
          if (o.hasOwnProperty(i)) {
            var a = o[i];
            console.log(a),
              a.text && (e += a.text),
              a.children && a.children.length && t(a.children);
          }
      })(t),
        wx.setClipboardData({
          data: e,
          success: function (t) {
            wx.showToast({
              title: '文字复制成功',
              icon: 'none',
              duration: 1e3,
            });
          },
        });
    },
    copyText: function (t) {
      var e = t.currentTarget.dataset.node,
        o = this;
      if (!wx.getStorageSync('notFistTimesCopyRich'))
        return (
          wx.setStorageSync('notFistTimesCopyRich', !0),
          void wx.showModal({
            title: '温馨提示',
            showCancel: !1,
            confirmText: '不再提示',
            content:
              '由于某些原因，导致某些客户无法长按复制文字，曲线救国的策略下我们采用了点击复制整段文字，麻烦各位老板自己粘贴后筛选一下需要的文字哦~',
            success: function (t) {
              t.confirm && o.handleNodeToTextAndCopy(e);
            },
          })
        );
      this.handleNodeToTextAndCopy(e);
    },
    loadImg: function (e) {
      var o = e.target.dataset.i;
      this.data.lazyLoad && !this.data.ctrl[o]
        ? this.setData(t({}, 'ctrl['.concat(o, ']'), 1))
        : this.data.loading &&
          2 != this.data.ctrl[o] &&
          this.setData(t({}, 'ctrl['.concat(o, ']'), 2));
    },
    linkpress: function (t) {
      var e = !0,
        o = t.currentTarget.dataset.attrs;
      (o.ignore = function () {
        return (e = !1);
      }),
        this.top.triggerEvent('linkpress', o),
        e &&
          (o['app-id']
            ? wx.navigateToMiniProgram({ appId: o['app-id'], path: o.path })
            : o.href &&
              ('#' == o.href[0]
                ? this.top.navigateTo({ id: o.href.substring(1) })
                : 0 == o.href.indexOf('http') || 0 == o.href.indexOf('//')
                  ? wx.setClipboardData({
                      data: o.href,
                      success: function () {
                        return wx.showToast({ title: '链接已复制' });
                      },
                    })
                  : wx.navigateTo({
                      url: o.href,
                      fail: function () {
                        wx.switchTab({ url: o.href });
                      },
                    })));
    },
    error: function (o) {
      var i = o.target.dataset.source,
        a = o.target.dataset.i,
        n = this.data.nodes[a];
      if ('video' == i || 'audio' == i) {
        var s = (n.i || 0) + 1;
        if (s < n.attrs.source.length)
          return this.setData(t({}, 'nodes['.concat(a, '].i'), s));
      } else
        'img' == i &&
          e &&
          (this.top.imgList.setItem(o.target.dataset.index, e),
          this.setData(t({}, 'nodes['.concat(a, '].attrs.src'), e)));
      this.top &&
        this.top.triggerEvent('error', {
          source: i,
          target: o.target,
          errMsg: o.detail.errMsg,
        });
    },
    loadVideo: function (e) {
      this.setData(
        t({}, 'nodes['.concat(e.target.dataset.i, '].attrs.autoplay'), !0),
      );
    },
    _downVideo: function (t) {
      var e = t.currentTarget.dataset.url;
      console.log(e),
        wx.showLoading({ title: '下载中' }),
        wx.downloadFile({
          url: e,
          success: function (t) {
            wx.hideLoading();
            var e = t.tempFilePath;
            wx.saveVideoToPhotosAlbum({
              filePath: e,
              success: function (t) {
                wx.showToast({
                  title: '下载成功',
                  icon: 'success',
                  duration: 800,
                });
              },
              fail: function (t) {
                wx.hideLoading(),
                  'saveVideoToPhotosAlbum:fail auth deny' === t.errMsg &&
                    wx.showModal({
                      title: '提示',
                      content: '需要您授权保存相册',
                      showCancel: !1,
                      success: function (t) {
                        wx.openSetting({
                          success: function (t) {
                            t.authSetting['scope.writePhotosAlbum']
                              ? wx.showModal({
                                  title: '提示',
                                  content: '获取权限成功,再次点击下载即可保存',
                                  showCancel: !1,
                                })
                              : wx.showModal({
                                  title: '提示',
                                  content: '获取权限失败，将无法保存到相册哦~',
                                  showCancel: !1,
                                });
                          },
                        });
                      },
                    });
              },
            });
          },
          fail: function (t) {
            wx.hideLoading(),
              wx.showToast({
                title: '获取文件路径失败',
                icon: 'none',
                duration: 2e3,
              }),
              console.warn('downloadFile err: ', t);
          },
        });
    },
  },
});
